package com.sunwayhorizo.health.ets.factory;


import com.sunwayhorizo.health.business.dto.DictionaryDTO;
import com.sunwayhorizo.health.common.DataTypeUtil;
import com.sunwayhorizo.health.hl7.common.Hl7Exception;
import com.sunwayhorizo.health.hl7.datatypes.CE;
import com.sunwayhorizo.health.hl7.persistent.RimCoreActCdSets;
import com.sunwayhorizo.health.hl7.persistent.RimCoreEntyCdSets;
import com.sunwayhorizo.health.hl7.persistent.RimCoreRoleCdSets;

public class DictionaryFactoryImpl extends DictionaryFactory {

    public DictionaryFactoryImpl() {
    }

    @Override
    public DictionaryDTO setDictionaryDTO(RimCoreEntyCdSets entyCdSets)
            throws Hl7Exception {
        DictionaryDTO dictionaryDTO = new DictionaryDTO();
        dictionaryDTO.setCodingschemeChName(entyCdSets.getCodingschemeChName());
        dictionaryDTO.setCodingschemeEnName(entyCdSets.getCodingschemeEnName());
        dictionaryDTO.setCodingschemeOid(entyCdSets.getCodingschemeOid());
        dictionaryDTO.setConceptCode(entyCdSets.getConceptCode());
        dictionaryDTO.setConceptName(entyCdSets.getConceptName());
        dictionaryDTO.setVersionName(entyCdSets.getCodingschemeVersionName());
        return dictionaryDTO;
    }

    @Override
    public DictionaryDTO setDictionaryDTO(RimCoreRoleCdSets roleCdSets)
            throws Hl7Exception {
        DictionaryDTO dictionaryDTO = new DictionaryDTO();
        dictionaryDTO.setCodingschemeChName(roleCdSets.getCodingschemeChName());
        dictionaryDTO.setCodingschemeEnName(roleCdSets.getCodingschemeEnName());
        dictionaryDTO.setCodingschemeOid(roleCdSets.getCodingschemeOid());
        dictionaryDTO.setConceptCode(roleCdSets.getConceptCode());
        dictionaryDTO.setConceptName(roleCdSets.getConceptName());
        dictionaryDTO.setVersionName(roleCdSets.getCodingschemeVersionName());
        return dictionaryDTO;
    }

    @Override
    public void setRimCoreEntyCdSet(RimCoreEntyCdSets entyCdSets,
            DictionaryDTO dictionaryDTO) throws Hl7Exception {
        if (dictionaryDTO == null) {
            // System.out.println("DictionaryFactoryImpl-setRimCoreEntyCdSet-DictionaryDTO is null!");
            return;
        }
        entyCdSets.setCodingschemeEnName(dictionaryDTO.getCodingschemeEnName());
        entyCdSets.setCodingschemeChName(dictionaryDTO.getCodingschemeChName());
        entyCdSets.setCodingschemeOid(dictionaryDTO.getCodingschemeOid());
        entyCdSets.setCodingschemeVersionName(dictionaryDTO.getVersionName());
        entyCdSets.setConceptCode(dictionaryDTO.getConceptCode());
        entyCdSets.setConceptName(dictionaryDTO.getConceptName());

    }

    @Override
    public void setRimCoreRoleCdSet(RimCoreRoleCdSets roleCdSets,
            DictionaryDTO dictionaryDTO) throws Hl7Exception {
        roleCdSets.setCodingschemeEnName(dictionaryDTO.getCodingschemeEnName());
        roleCdSets.setCodingschemeChName(dictionaryDTO.getCodingschemeChName());
        roleCdSets.setCodingschemeOid(dictionaryDTO.getCodingschemeOid());
        roleCdSets.setCodingschemeVersionName(dictionaryDTO.getVersionName());
        roleCdSets.setConceptCode(dictionaryDTO.getConceptCode());
        roleCdSets.setConceptName(dictionaryDTO.getConceptName());

    }

    @Override
    public void setRimCoreActCdSet(RimCoreActCdSets actCdSets,
            DictionaryDTO dictionaryDTO) throws Hl7Exception {
        if (null == actCdSets || null == dictionaryDTO)
            return;
        actCdSets.setCodingschemeEnName(dictionaryDTO.getCodingschemeEnName());
        actCdSets.setCodingschemeChName(dictionaryDTO.getCodingschemeChName());
        actCdSets.setCodingschemeOid(dictionaryDTO.getCodingschemeOid());
        actCdSets.setCodingschemeVersionName(dictionaryDTO.getVersionName());
        actCdSets.setConceptCode(dictionaryDTO.getConceptCode());
        actCdSets.setConceptName(dictionaryDTO.getConceptName());

    }

    @Override
    public DictionaryDTO convertActCdSets(RimCoreActCdSets actCdSets)
            throws Hl7Exception {

        DictionaryDTO dictionaryDTO = new DictionaryDTO();
        if (null == actCdSets) {
            return dictionaryDTO;
        }
        dictionaryDTO.setCodingschemeChName(actCdSets.getCodingschemeChName());
        dictionaryDTO.setCodingschemeEnName(actCdSets.getCodingschemeEnName());
        dictionaryDTO.setCodingschemeOid(actCdSets.getCodingschemeOid());
        dictionaryDTO.setConceptCode(actCdSets.getConceptCode());
        dictionaryDTO.setConceptName(actCdSets.getConceptName());
        dictionaryDTO.setVersionName(actCdSets.getCodingschemeVersionName());
        return dictionaryDTO;
    }

    /**
     * 把DictionaryDTO转换为CE类型以供业务代码中调用
     * 
     * @param dictionaryDTO
     *            术语字典信息
     * @return CE
     * @throws Hl7Exception
     */
    public CE getCeByDictionaryDTO(DictionaryDTO dictionaryDTO)
            throws Hl7Exception {
        
        if(dictionaryDTO == null) return DataTypeUtil.getCE("");
        CE ce = DataTypeUtil.getCE(dictionaryDTO.getConceptCode(),
                dictionaryDTO.getCodingschemeOid(),
                dictionaryDTO.getCodingschemeEnName(),
                dictionaryDTO.getCodingschemeChName(),
                dictionaryDTO.getConceptName(), dictionaryDTO.getVersionName());
        return ce;
    }
    
    public DictionaryDTO getDictionaryDTOByCe(CE ce) throws Hl7Exception
    {
        DictionaryDTO dictionaryDTO = new DictionaryDTO();
        if (null == ce) 
        {
            return dictionaryDTO;
        }
       
        if(ce.codeSystemChName() != null && !ce.codeSystemChName().toString().equals("<Null:NI>") )
        {
            dictionaryDTO.setCodingschemeChName(ce.codeSystemChName().toString());
        }
        if(ce.codeSystemEnName() != null && !ce.codeSystemEnName().toString().equals("<Null:NI>"))
        {
            dictionaryDTO.setCodingschemeEnName(ce.codeSystemEnName().toString());
        }
        if(ce.code() != null && !ce.code().toString().equals("<Null:NullFlavor: NI>"))
        {
            dictionaryDTO.setConceptCode(ce.code().toString());
        }
        if(ce.codeName() != null && !ce.codeName().toString().equals("<Null:NI>"))
        {
            dictionaryDTO.setConceptName(ce.codeName().toString());
        }
        if(ce.codeSystemVersion() != null && !ce.codeSystemVersion().toString().equals("<Null:NI>"))
        {
            dictionaryDTO.setVersionName(ce.codeSystemVersion().toString());
        }
        return dictionaryDTO;
    }

}
